package com.yang.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.reggie.common.CustomException;
import com.yang.reggie.dto.DishDto;
import com.yang.reggie.entity.Dish;
import com.yang.reggie.entity.DishFlavor;
import com.yang.reggie.entity.SetmealDish;
import com.yang.reggie.mapper.DishMapper;
import com.yang.reggie.service.DishFlavorService;
import com.yang.reggie.service.DishService;
import com.yang.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品数据，分别插入菜品表和菜品口味表：dish、dish_flavor
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //新增菜品表基本信息
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();//菜品口味
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //新增菜品口味数据
        dishFlavorService.saveBatch(flavors);
    }

    /**
     *根据id查询菜品信息和口味信息，修改回返数据
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //菜品查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //口味查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //跟新菜品表
        this.updateById(dishDto);

        //清理菜品口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapperDishFlavor = new LambdaQueryWrapper<>();
        queryWrapperDishFlavor.in(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapperDishFlavor);

        //添加菜品口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据ID修改启用状态
     * @param status
     * @param ids
     * @return
     */
    @Override
    public void updateByIdWithFlavor(int status, List<Long> ids) {
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Dish::getId,ids);
        updateWrapper.set(Dish::getStatus,status);
        dishService.update(updateWrapper);
        if (status == 0){
            //删除套餐关系
            LambdaQueryWrapper<SetmealDish> queryWrapperDish = new LambdaQueryWrapper<>();
            queryWrapperDish.in(SetmealDish::getDishId,ids);
            setmealDishService.remove(queryWrapperDish);
        }

    }

    /**
     * 根据id删除菜品
     * @param ids
     * @return
     */
    @Override
    public void removeWithFlavor(List<Long> ids) {
        //查询状态停售可删
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);

        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("正在售卖中，不能删除");
        }

        this.removeByIds(ids);

        //删除口味
        LambdaQueryWrapper<DishFlavor> queryWrapperDishFlavor = new LambdaQueryWrapper<>();
        queryWrapperDishFlavor.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapperDishFlavor);

        //删除套餐关系
        LambdaQueryWrapper<SetmealDish> queryWrapperSetmealDish = new LambdaQueryWrapper<>();
        queryWrapperSetmealDish.in(SetmealDish::getDishId,ids);
        setmealDishService.remove(queryWrapperSetmealDish);

    }
}
